using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using Umbraco.Ecommerce.BusinessLogic.Collections;

namespace Umbraco.Ecommerce.BusinessLogic
{
	/// <summary>
	/// Summary description for Product.
	/// </summary>
	public class Product
	{
		private int _NodeId;
		private bool _UseMultipleProductVariants = false;
		private ProductVariantCollection v;
		private string _Name;
		private int _weight;

		public Product(int NodeId)
		{
			_NodeId = NodeId;
			ensureProduct();
			_Name = SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,"select Name from ecommerceProduct where NodeId = " + _NodeId).ToString();
		}

		public int NodeId 
		{
			get 
			{
				return _NodeId;
			}
		}

		public bool IsProduct(int NodeId) {
				return (int.Parse(SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,"Select count(NodeId) as tmp from ecommerceProduct where NodeId = " + NodeId).ToString()) > 0);
		}

		public string Name {
			get {return _Name;}
			set {
			  _Name = value;
			  SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN,CommandType.Text,"Update ecommerceProduct set  Name = @Name where nodeId = @NodeId",new SqlParameter("@NodeId", _NodeId), new SqlParameter("@Name", value));
       		 }
		}

		public int Weight 
		{
			get {return _weight;}
			set 
			{
				_weight = value;
				SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN,CommandType.Text,"Update ecommerceProduct set  Weight = @Weight where nodeId = @NodeId",new SqlParameter("@NodeId", _NodeId), new SqlParameter("@Weight", value));
			}
		}

		private void ensureProduct() 
		{
			if (!IsProduct(_NodeId)) {
				SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN,CommandType.Text,
					"Insert into ecommerceProduct (NodeId, Name) values (@NodeId,@Name)",
						new SqlParameter("@NodeId", _NodeId),
							new SqlParameter("@Name", new umbraco.cms.businesslogic.CMSNode(_NodeId).Text)
						);
            }
		}

		public bool UseMultipleProductVariants 
		{
			get
			{
				return bool.Parse(SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,"select useMultipleProductVariants from ecommerceProduct where NodeId = " +_NodeId).ToString());
			}	
			set 
			{
				_UseMultipleProductVariants = value;

				SqlParameter[] SqlParams = new SqlParameter[] {
				   new SqlParameter("@value", value),
				   new SqlParameter("@NodeId", _NodeId)
					};
					SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN,CommandType.Text,"Update ecommerceProduct set useMultipleProductVariants = @value  where NodeId = @NodeId",SqlParams);
				}
		}

		private void loadProductVariants() {
			if (v!=null)
				v.Clear();
			else {
				v = new ProductVariantCollection();
			}
			SqlDataReader dr = SqlHelper.ExecuteReader(umbraco.GlobalSettings.DbDSN,CommandType.Text,"select Id from ecommerceProductVariants where NodeId = " + _NodeId + " order by [order]");
				while (dr.Read()) 
					v.Add(new ProductVariant(int.Parse(dr["id"].ToString())));
				dr.Close();
		}


		public ProductVariantCollection ProductVariants {
			get {
				if (v == null) {
					v = new ProductVariantCollection();
					loadProductVariants();
				}
				if (v.Count == 0) {
					// A product must at all times have at least one ProductVariant
					// therefor we create the first if no-one exists (only just after the product has been created)
					if (!AddProductVariant("Node: " + _NodeId,"New product",0))
						AddProductVariant(Guid.NewGuid().ToString(),"New product", 0);
					loadProductVariants();
				}	
				return v;
			}
		}

		public bool AddProductVariant(string ProductId, string Name, decimal Price) {
			if (ProductVariant.isUniqueProcuctId(ProductId))  {
				int order = 0;
				if (v != null) order = v.Count;
				SqlParameter[] SqlParams = new SqlParameter[] {
													  new SqlParameter("@NodeId",_NodeId),
													  new SqlParameter("@Name", Name),
													  new SqlParameter("@ProductId", ProductId),
													  new SqlParameter("@Price", Price),
													  new SqlParameter("@Order", order + 1)
													 };
					SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN,CommandType.Text,"insert into ecommerceProductVariants (NodeId, Name,ProductId,Price,[Order]) values (@NodeId,@Name,@ProductId,@Price,@Order)",SqlParams);
					
				loadProductVariants();
				return true;
				} 
			return false;
		}

		public  void RemoveProductVariant(int Id) {
			SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN,CommandType.Text,"delete from ecommerceProductVariants where Id = @id",new SqlParameter("@Id", Id));
			loadProductVariants();
			for (int i = 0; i < ProductVariants.Count;i++)
				ProductVariants[i].Order = i+1;
		}
	}
}